package cn.yikaojiuguo.student.ui.view;

import android.app.Activity;
import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

public class TimeScaleView extends LinearLayout {

	private View view;
	private LinearLayout.LayoutParams layoutParams;
	/**
	 * 总时长
	 */
	private long millisInFuture;

	/**
	 * 时间间隔
	 */
	private long countDownInterval;
	/**
	 * 暂停时的剩余时长
	 */
	private long leftTime;
	private int width;
	private int reduceWidth;
	private TimeCount timeCount;
	private float viewInitWidth;

	public TimeScaleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context);
	}

	public TimeScaleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public TimeScaleView(Context context) {
		super(context);
		initView(context);
	}

	private void initView(Context context) {
		this.setBackgroundColor(0xFFFFFFFF);
		width = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
		view = new View(context);
		view.setBackgroundColor(0xff24A2FE);
		this.addView(view);
		layoutParams = (LayoutParams) view.getLayoutParams();
		layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
		layoutParams.height = dip2px(5);
		this.setOrientation(LinearLayout.HORIZONTAL);
		this.setGravity(Gravity.CENTER_HORIZONTAL);
		view.setLayoutParams(layoutParams);
		view.setVisibility(View.INVISIBLE);
	}

	public void setHeightPx(int height) {
		layoutParams.height = height;
		view.setLayoutParams(layoutParams);
	}

	public void setViewColor(int color) {
		view.setBackgroundColor(color);
	}

	public void setHeightDp(float height) {
		layoutParams.height = dip2px(height);
		view.setLayoutParams(layoutParams);
	}

	public int dip2px(float dpValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public void setTime(long millisInFuture, long countDownInterval) {
		this.millisInFuture = millisInFuture;
		this.countDownInterval = countDownInterval;
	}

	public void start() {
		view.setVisibility(view.VISIBLE);
		layoutParams.width = width;
		view.setLayoutParams(layoutParams);
		if (countDownInterval == 0) {
			millisInFuture = 120000;
			countDownInterval = millisInFuture / width;
		}
		leftTime = millisInFuture;
		viewInitWidth = width;
		timeCount = new TimeCount(millisInFuture, countDownInterval);
		reduceWidth = 1;
		timeCount.start();
	}

	public void stop() {
		timeCount.cancel();
	}

	public void reStart() {
		view.setVisibility(View.VISIBLE);
		timeCount = new TimeCount(leftTime, countDownInterval);
		timeCount.start();
	}

	private class TimeCount extends CountDownTimer {

		/**
		 * @param millisInFuture
		 *            总时长
		 * @param countDownInterval
		 *            时间间隔
		 * 
		 * */
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		/**
		 * 计时完成
		 * */
		@Override
		public void onFinish() {
			view.setVisibility(view.INVISIBLE);
		}

		/**
		 * 计时过程中显示
		 * 
		 * */
		@Override
		public void onTick(long millisUntilFinished) {
			float percent = ((float) millisUntilFinished) / millisInFuture;
			layoutParams.width = (int) (viewInitWidth * percent);
			if (layoutParams.width >= 0) {
				view.setLayoutParams(layoutParams);
			}
			leftTime -= countDownInterval;
		}
	}
}
